/*
 * Project: yygh-parent
 *
 * File Created at 2023/7/9
 *
 * 自定义时间格式代替DATE标签：2023-07-09 18:03:18:03
 */
package com.atguigu.yygh.order.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.order.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 用户名
 * @Type DictController.java
 * @Desc
 * @date 2023/7/9 18:03
 */
@Api("字典管理")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    DictService dictService;
    //通过id找到子节点
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> childData = dictService.findChildData(id);
        return Result.ok(childData);
    }
    //导出数据字典
    @GetMapping("/exportData")
    public void export(HttpServletResponse response) throws IOException {
        dictService.exportData(response);

    }
    //导入数据字典
    @PostMapping("/importData")
    public Result importData(MultipartFile file) throws IOException {
        dictService.importData(file);
        return Result.ok();
    }

    //TODO 写controller进行feign的调用
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getNameByValueAndParentId(
              @ApiParam(name = "value", value = "值", required = true) @PathVariable Integer value,
              @ApiParam(name = "parentDictCode", value = "上级编码", required = true) @PathVariable Integer parentDictCode) {
        return dictService.getNameByValueAndParentId(value,parentDictCode);
    }
    @ApiOperation(value = "通过string获取数据字典名称")
    @GetMapping(value = "/getNameByString/{parentDictCode}/{value}")
    public String getNameByValueAndParentId(
            @ApiParam(name = "value", value = "值", required = true) @PathVariable String value,
            @ApiParam(name = "parentDictCode", value = "上级编码", required = true) @PathVariable String parentDictCode) {
        return dictService.getNameByValueAndParentIdByString(value,parentDictCode);
    }




    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getNameByValue(
            @ApiParam(name = "value", value = "值", required = true) @PathVariable Integer value){
        return dictService.getNameByValue(value);
    }
    //2023 8/23 18:26
    @ApiOperation(value = "获取数据字典名称 通过字符串")
    @GetMapping(value = "getNameByString10/{value}")
    public String getNameByString10(
            @ApiParam(name = "value", value = "值", required = true) @PathVariable String value){

        return dictService.getNameByStringValue(value);
    }

    @ApiOperation(value = "获取详细地址名称")
    @GetMapping(value = "getDetailAddress/{provinceCode}/{cityCode}/{districtCode}")
    public String getDetailAddress(
            @ApiParam(name = "provinceCode", value = "省", required = true)  @PathVariable  Integer provinceCode,
            @ApiParam(name = "cityCode", value = "市", required = false)   @PathVariable Integer cityCode
            ,@ApiParam(name = "districtCode", value = "区", required = false)   @PathVariable Integer districtCode) {
        return dictService.getDetailAddress(provinceCode,cityCode,districtCode);
    }

    //身份证可能超出int范围
    //获得父编号得到value
    @ApiOperation(value = "获得父编号得到value")
    @GetMapping(value = "findByDictCode/{dictCode}")
    public Result findByParent(@PathVariable Integer dictCode){
        List<Dict> namesBydictCode = dictService.getNamesBydictCode(dictCode);

        return Result.ok(namesBydictCode);
    }

    @ApiOperation(value = "查询dictCode查询下级数据字典")
    @GetMapping(value = "findByDictCode1/{dictCode}")
    public Result findByDictCode1(@PathVariable String dictCode){
        List<Dict> namesBydictCode = dictService.getNamesBydictCode1(dictCode);


        return Result.ok(namesBydictCode);
    }

    @ApiOperation(value = "通过父id查询地区子list")
    @GetMapping(value = "findChildData2/{dictCode}")
    public Result findChildData(@PathVariable Integer dictCode){
        List<String> namesBydictCode = dictService.getNamesByChildDictCode(dictCode);

        return Result.ok(namesBydictCode);
    }

    @ApiOperation(value = "通过父id查询地区子list")
    @GetMapping(value = "findChildData1/{dictCode}")
    public Result findChildData1(@PathVariable Integer dictCode){
        List<String> namesBydictCode = dictService.getNamesByChildDictCode1(dictCode);

        return Result.ok(namesBydictCode);
    }
    //2023 8/23 18:08

    @ApiOperation(value = "通过value和DicCode查询name")
    @GetMapping(value = "getNameByValueAndDicCode/{value}/{dictCode}")
    public String getNameByValueAndDicCode(@PathVariable String value,@PathVariable String dictCode){
        String nameByValueAndDicCode = dictService.getNameByValueAndDicCode(value, dictCode);
        return nameByValueAndDicCode;
    }

}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * <p>
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2023/7/9 用户名 create
 */ 
